package com.wmm.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.wmm.entity.User;
import com.wmm.entity.UserDTO;
import com.wmm.service.UserService;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

@RestController
@RequestMapping("user")
public class UserController {

    @Reference
    private UserService userService;

    @GetMapping("login")
    public UserDTO login(String phone, String password){

        UserDTO userDTO = new UserDTO();
        User user = null;
        System.out.println("phone =" +phone);
        System.out.println("password = "+ password);
        // 检测手机号是否注册
        Integer i = userService.checkPhone(phone);
        if (i==0){

            // 未注册，自动注册并登录
            userService.register(phone, password);
            userDTO.setMessage("手机号尚未注册，系统已帮您自动注册，请牢记密码！");
            user = userService.login(phone, password);

        }else {
            user = userService.login(phone, password);
            System.out.println(user);
            if(user == null){

                userDTO.setState(300);
                userDTO.setMessage("帐号密码不匹配，登录失败！");
            }else {
                userDTO.setState(200); //200表示成功
                userDTO.setMessage("登录成功！");
            }
        }

        userDTO.setContent(user);
        return userDTO;
    }


    /**
     * 修改图片路径
     * @param file
     * @return
     */
    @RequestMapping("uploadUserImage")
    public UserDTO uploadUserImage(MultipartFile file){

        // 返回结果
        UserDTO res = new UserDTO();

        try {
            // 处理上传文件
            if (file == null) {

                res.setState(500);
                res.setMessage("上传文件不能为空");
                return res;
            }

            // 获取文件类型
            String oldFileName = file.getOriginalFilename();
            String suffix = oldFileName.substring(oldFileName.lastIndexOf(".") + 1);

            // 生产唯一文件名
            String newFileName = UUID.randomUUID().toString() + "." + suffix;

            // FastDFS 初始化
            ClientGlobal.initByProperties("config/fastdfs-client.properties");
            TrackerClient trackerClient = new TrackerClient();
            TrackerServer trackerServer = trackerClient.getConnection();
            StorageServer storageServer = null;
            StorageClient1 client = new StorageClient1(trackerServer, storageServer);

            // 设置文件元信息
            NameValuePair[] metaList = new NameValuePair[2];
            metaList[0] = new NameValuePair("originalFileName", oldFileName);
            metaList[1] = new NameValuePair("fileName", newFileName);

            // 上传文件
            String fileId = client.upload_file1(file.getBytes(), suffix, metaList);

            System.out.println(fileId);

            res.setState(200);
            res.setMessage("文件上传成功");
            res.setContent(fileId);

            // 释放资源
            trackerServer.close();

        } catch (Exception e) {

            e.printStackTrace();
            res.setState(500);
            res.setMessage(e.getMessage());
        }

        // 返回结果
        return res;
    }


    @RequestMapping("/updateUserInfo")
    public UserDTO updateUserInfo(@RequestBody User user) throws Exception {
        System.out.println(user.getId());
        System.out.println(user.getName());
        System.out.println(user.getPortrait());
        UserDTO userDTO = new UserDTO();
        Integer i = userService.updateUserInfo(user.getId(), user.getName(), user.getPortrait());
        if(i > 0) {
            userDTO.setState(200);
            userDTO.setMessage("修改成功");
        } else {
            userDTO.setState(400);
            userDTO.setMessage("修改失败");
        }
        return userDTO;



    }

    @RequestMapping("/updatePassword")
    public UserDTO updatePassword(@RequestBody User user) {
        System.out.println(user.getId());
        System.out.println(user.getPassword());
        UserDTO userDTO = new UserDTO();
        int i = userService.updatePassword(user.getId(),user.getPassword());
        if(i > 0) {
            userDTO.setState(200);
            userDTO.setMessage("修改成功");
        } else {
            userDTO.setState(400);
            userDTO.setMessage("修改失败");
        }
        return userDTO;
    }
}
